<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="org.springframework.context.ApplicationContext"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="org.springframework.web.servlet.support.RequestContextUtils" %>
<%@ page import="org.springframework.security.core.context.SecurityContextHolder" %>
<%@ page import="org.springframework.security.core.userdetails.UserDetails" %>
<%@ page import="it.cst.entity.*" %>
<%@ page import="it.cst.entity.service.*" %>
<%@ page import="it.cst.util.Roles" %>
<%
    ApplicationContext ctx = RequestContextUtils.getWebApplicationContext(request);
    UserService us = (UserService) ctx.getBean("userService");
    RoleService rs = (RoleService) ctx.getBean("roleService");
	Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	UserDetails userDetails = null;
	userDetails = (UserDetails) principal;
	String userName = userDetails.getUsername();
	User user = us.getUser(userName);
	Roles roles = Roles.getInstance(rs);
%>
<!DOCTYPE html> 
<html> 
	<head> 
	<title>CST</title> 
	<meta name="viewport" content="width=device-width, initial-scale=1"> 
	<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0-beta.1/jquery.mobile-1.2.0-beta.1.min.css" />
	<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
	<script src="http://code.jquery.com/mobile/1.2.0-beta.1/jquery.mobile-1.2.0-beta.1.min.js"></script>
	<script>
	function saveUser(){
		document.getElementById("userProfile").submit();
	}
	</script>
	</head>
	<body> 
<div data-role="page">
	<div data-role="header">
		<h1>Profilo Utente</h1>
	</div><!-- /header -->
	<div data-role="content">	
		<form name="userProfile" id="userProfile" method="POST" action="saveUser" data-ajax="false">
			<div data-role="fieldcontain">
			   <table style="width:100%;align:center;border-collapse:collapse">
				   	<tr>
				   		<td style="width:35%;text-align:left"><label for="username"><b>UserName :</b></label></td>
				   		<td style="width:65%;text-align:left"><input type="text" name="username" id="username" value="<%=user.getUsername()%>" READONLY/></td>
				   	</tr>
				   	<tr>
				   		<td style="width:35%;text-align:left"><label for="firstname"><b>Nome :</b></label></td>	
				   		<td style="width:65%;text-align:left"><input type="text" name="firstname" id="firstname" value="<%=user.getFirstname()%>"/></td>
				   	</tr>
				   	<tr>
				   		<td style="width:35%;text-align:left"><label for="lastname"><b>Cognome :</b></label></td>	
				   		<td style="width:65%;text-align:left"><input type="text" name="lastname" id="lastname" value="<%=user.getLastname()%>"/></td>
				   	</tr>
				   	<tr>
				   		<td style="width:35%;text-align:left"><label for="email"><b>Email :</b></label></td>	
				   		<td style="width:65%;text-align:left"><input type="text" name="email" id="email" value="<%=user.getEmail()%>"/></td>
				   	</tr>
				   	<tr>
				   		<td style="width:35%;text-align:left"><label for="role"><b>Ruolo Pref. :</b></label></td>	
				   		<td style="width:65%;text-align:left">
					   		<select name="role" id="role" data-native-menu="false">
						      <%for(Role role : roles.getRoles())
						      {
							      	if(role.getId().compareTo(user.getDefaultrole()) == 0)
							      	{
							      		%>
							      		<option selected value="<%=role.getRole()%>"><%=role.getRole()%></option>
							      		<% 
							      	}
							      	else
							      	{
							      		%>
							      		<option value="<%=role.getRole()%>"><%=role.getRole()%></option>
							      		<%
							      	}
						      }%>
						    </select>
				   		</td>
				   	</tr>
				   	<tr>
				   		<td style="width:35%;text-align:left"><label for="notify"><b>Notifica :</b></label></td>	
				   		<td style="width:65%;text-align:left">
					   		<select name="notify" id="notify" data-native-menu="false">
							      	<%if(user.getNotify())
							      	{
							      		%>
							      		<option selected value="true">Sì</option>
							      		<option value="false">No</option>
							      		<% 
							      	}
							      	else
							      	{%>
							      		<option selected value="false">No</option>
							      		<option value="true">Sì</option>
							      	<%}%>
						    </select>
				   		</td>
				   	</tr>				   	
			   </table>
			   <br>
			   <br>
			   <table style="width:100%;align:center;border-collapse:collapse">
			   	<tr>
			   		<td>
			   			<center><input type="button" value="Salva" onclick="saveUser()" data-ajax="false"/></center>
			   		</td>
			   	</tr>
			   	<tr>
			   		<td>
			   			<center><a href="changePwd" data-role="button" data-ajax="false">Cambio password</a></center>
			   		</td>
			   	</tr>   	
			   	<tr>
			   		<td>
			   			<center><a href="home" data-role="button" data-ajax="false">Home</a></center>
			   		</td>
			   	</tr>
			   </table>
			</div>
		</form>	
	</div><!-- /content -->
</div><!-- /page -->
</body>
</html>